package com.feriaderemates.dao.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.feriaderemates.domain.userAccount.EmailCommunicationSettings;
import com.feriaderemates.domain.userAccount.User;
import com.feriaderemates.domain.userAccount.UserAddress;

public class UserPreferencesAndSettingsDaoInterfaceDao extends JdbcDaoSupport implements
		UserPreferencesAndSettingsDaoInterface {

	//Query
	private static String UPDATE_USER_ADDRESS = "UPDATE portal.address set street_1 = ?, street_2 = ?," +
			" zip_code = ? , city = ?, neighborhood = ?, address_type = ? where user_id = ?";
	private static String GET_USER_ADDRESS = "SELECT * FROM portal.address where user_id = ?";
	private static String ADD_USER_ADDRESS = "INSERT INTO portal.address (street_1,street_2,zip_code,city,neighborhood,address_type,user_id)" +
			" VALUES(?,?,?,?,?,?,?) ";
	private static String DELETE_ADDRESS="DELETE from portal.address where address_id = ?";
	@Override
	public void updateUserAddress(final UserAddress address) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
               PreparedStatement ps = connection.prepareStatement(UPDATE_USER_ADDRESS);
               ps.setString(1, address.getStreet1());
               ps.setString(2, address.getStreet2());
               ps.setString(3, address.getZipCode());
               ps.setString(4, address.getCity());
               ps.setString(5, address.getNeighborhood());
               ps.setString(6, address.getUserAddressType().name());
               ps.setInt(7, address.getUserId());
               return ps;
            }
        });

	}

	@Override
	public List<UserAddress> getUserAddresses(int userId) {
		return getJdbcTemplate().query(GET_USER_ADDRESS,new Object[]{userId}, new UserAddressRowMapper());
	}

	@Override
	public EmailCommunicationSettings getEmailCommunicationSettings(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEmailCommunicationSettings(EmailCommunicationSettings settings) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addUserAddress(final UserAddress address) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
               PreparedStatement ps = connection.prepareStatement(ADD_USER_ADDRESS);
               ps.setString(1, address.getStreet1());
               ps.setString(2, address.getStreet2());
               ps.setString(3, address.getZipCode());
               ps.setString(4, address.getCity());
               ps.setString(5, address.getNeighborhood());
               ps.setString(6, address.getUserAddressType().name());
               ps.setInt(7, address.getUserId());
               return ps;
            }
        });

	}

	@Override
	public void deleteUserAddress(final int addressId) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
               PreparedStatement ps = connection.prepareStatement(DELETE_ADDRESS);
               ps.setInt(1, addressId);
               return ps;
            }
        });

	}

}
